﻿using System.ServiceModel;
using UtfprLibrary.Communication.Contracts;
using UtfprLibrary.Exceptions;
using System;

namespace UtfprLibrary.Communication.Behaviors {
    /// <summary>
    /// TODO
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Subscription : ISubscription {
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="subject"></param>
        public void Subscribe(string subject) {
            try {
                IPublishing subscriber =
                    OperationContext.Current.GetCallbackChannel<IPublishing>();
                Filter.AddSubscriber(subject, subscriber);
            } catch (Exception ex) {
                throw new UtfprCommunicationException(Properties.Resources.Communication_Subscription_ExMsg_Subscribe, ex);
            }
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="subject"></param>
        public void UnSubscribe(string subject) {
            try {
                IPublishing subscriber =
                    OperationContext.Current.GetCallbackChannel<IPublishing>();
                Filter.RemoveSubscriber(subject, subscriber);
            } catch (Exception ex) {
                throw new UtfprCommunicationException(Properties.Resources.Communication_Subscription_ExMsg_UnSubscribe, ex);
            }
        }
    }
}